home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / cmordarr.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-06  |  5.5 KB  |  125 lines

  1. // CmOrdArr.h
  2. // -----------------------------------------------------------------
  3. // Compendium - C++ Container Class Library
  4. // Copyright (C) 1992-1994, Glenn M. Poorman, All rights reserved
  5. // -----------------------------------------------------------------
  6. // Ordered array definition.
  7. // -----------------------------------------------------------------
  8.  
  9. #ifndef _CMORDARR_H
  10. #define _CMORDARR_H
  11.  
  12. #include <cm/include/cmcont.h>
  13.  
  14. class CmOrderedArrayIterator;                  // Iterator class stub.
  15.  
  16. class CmOrderedArray : public CmContainer {    // Ordered array definition.
  17. public:
  18.   CmOrderedArray(unsigned = 0, unsigned = 0);  // Default array constructor.
  19.   CmOrderedArray(const CmOrderedArray&);       // Array copy constructor.
  20.  ~CmOrderedArray();                            // Array destructor.
  21.  
  22.   CmOrderedArray& operator=(const CmOrderedArray&);  // Assignment operator.
  23.  
  24.   CmObject* operator[](int) const;             // Indexing operator.
  25.                                                // (Cannot be used as lvalue).
  26.  
  27.   void        delta      (unsigned);           // Set delta value.
  28.   unsigned    delta      () const;             // Get delta value.
  29.   int         total      () const;             // Return number of objects.
  30.   CmObject*   at         (int) const;          // Get object at index.
  31.   Bool        add        (CmObject*);          // Add object to array in order.
  32.   Bool        remove     (CmObject*);          // Remove equal object.
  33.   Bool        removeAt   (int);                // Remove object at index.
  34.   int         index      (CmObject*) const;    // Get index of object.
  35.   int         shouldGo   (CmObject*) const;    // Get index for potential obj.
  36.   CmObject*   lookup     (CmObject*) const;    // Look for equal object.
  37.   Bool        contains   (CmObject*) const;    // Object is in array?
  38.   unsigned    occurrences(CmObject*) const;    // How many occurrences?
  39.   void        removeAll  ();                   // Remove all objects.
  40.   Bool        resize     (unsigned);           // Resize the array.
  41.   Bool        isEmpty    () const;             // Is array empty?
  42.   CmIterator* newIterator() const;             // Get array iterator.
  43.  
  44.   Bool write(CmReserveFile&) const;            // Write to reserve file.
  45.   Bool read (CmReserveFile&);                  // Read from reserve file.
  46.  
  47.   CMOBJECT_DEFINE(CmOrderedArray, CmContainer) // Define object funcs.
  48.  
  49. protected:
  50.   unsigned   _delta;                           // Delta value.
  51.   unsigned   _total;                           // Number of objects.
  52.   CmObject **_entries;                         // Array of object pointers.
  53.   friend     CmOrderedArrayIterator;           // Iterator can access,
  54. };
  55.  
  56. class CmOrderedArrayIterator : public CmIterator {  // Iterator definition.
  57. public:
  58.   CmOrderedArrayIterator(const CmOrderedArray& A)   // Iterator constructor.
  59.                  : _array(A), _index(0) {}
  60.  
  61.   Bool      done    () const;                  // Check if done iterating.
  62.   CmObject* next    ();                        // Return and advance.
  63.   CmObject* previous();                        // Return and backup.
  64.   CmObject* current () const;                  // Return current object.
  65.   void      first   ();                        // Move to first object.
  66.   void      last    ();                        // Move to last object.
  67.  
  68.   CMOBJECT_DEFINE(CmOrderedArrayIterator, CmIterator) // Define object funcs.
  69.  
  70. protected:
  71.   const CmOrderedArray& _array;                // Array being iterated.
  72.   int                   _index;                // Current array index.
  73.   friend                CmOrderedArray;        // Array class can access.
  74. };
  75.  
  76. // "delta" sets a new delta value for automatic growing.
  77. inline void CmOrderedArray::delta(unsigned dt)
  78. { _delta = dt; }
  79.  
  80. // "delta" returns the current delta value.
  81. inline unsigned CmOrderedArray::delta() const
  82. { return _delta; }
  83.  
  84. // "total" returns the number of objects added to this array.
  85. inline int CmOrderedArray::total() const
  86. { return _total; }
  87.  
  88. // "at" returns the object at the specified index.
  89. inline CmObject* CmOrderedArray::at(int idx) const
  90. { return (idx >= 0 && idx < _total) ? _entries[idx] : NULL; }
  91.  
  92. // "[]" returns the object at the specified index.  Cannot be used as lvalue.
  93. inline CmObject* CmOrderedArray::operator[](int idx) const
  94. { return at(idx); }
  95.  
  96. // "contains" checks if the array contains an object equal to the input.
  97. inline Bool CmOrderedArray::contains(CmObject* pObj) const
  98. { return (index(pObj) > -1) ? TRUE : FALSE; }
  99.  
  100. // "done" checks to see if we can iterate any further.
  101. inline Bool CmOrderedArrayIterator::done() const
  102. { return (_index >= _array._total || _index < 0); }
  103.  
  104. // "next" returns the current object and advances the iterator.
  105. inline CmObject* CmOrderedArrayIterator::next()
  106. { return (_index < _array._total) ? _array._entries[_index++] : NULL; }
  107.  
  108. // "previous" returns the current object and decrements the iterator.
  109. inline CmObject* CmOrderedArrayIterator::previous()
  110. { return (_index >= 0) ? _array._entries[_index--] : NULL; }
  111.  
  112. // "current" returns the current object pointed to by the iterator.
  113. inline CmObject* CmOrderedArrayIterator::current() const
  114. { return (_index < _array._total) ? _array._entries[_index] : NULL; }
  115.  
  116. // "first" moves the iterator to the first object.
  117. inline void CmOrderedArrayIterator::first()
  118. { _index = 0; }
  119.  
  120. // "last" moves the iterator to the last object.
  121. inline void CmOrderedArrayIterator::last()
  122. { _index = _array._total-1; }
  123.  
  124. #endif
  125.